<template>
        <el-dialog  :title="title" :fullscreen="dialogFull" v-model="showSysRoleEdit" v-drag>
            <el-form ref="sysRoleEditRef" :rules="rules" :model="formObj" :label-position="labelPosition">
                <el-form-item prop="roleName"  label="角色名称" :label-width="formLabelWidth">
                    <el-input v-model="formObj.roleName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="上级角色" :label-width="formLabelWidth">
                    <el-select v-model="parentName" placeholder="上级角色" style="width: 100%" @change="_pNameChange">
                        <div v-for="item in pNameList">
                            <el-option  :key="item.id" :label="item.roleName" :value="item.id"></el-option>
                        </div>
                    </el-select>
                </el-form-item>
            </el-form>

            <template #footer>
    <span class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </span>
            </template>
        </el-dialog>

</template>

<script>


    import {getReq, clearObject, postReq, putReq} from "../../../utils/api"
    import {ElMessage} from "element-plus";
    export default {
        name: "SysMenuEdit",
        components:{},
        mounted() {

        },
        data(){
            return{
                labelPosition:'right',
                formObj:{
                    pid:undefined,
                    roleName:undefined,
                    parentName:undefined
                },
                id:undefined,
                pNameList:[],
                parentName: undefined,
                showSysRoleEdit:false,
                title:'',
                dialogFull:false,
                formLabelWidth: '150px',
                rules: {
                    roleName: [
                        { required: true, message: '请输入角色名称', trigger: 'blur' }
                    ]
                }
            }
        },
        methods:{
            open(id){
                this.parentName = undefined
                clearObject(this.formObj)
                if (undefined!==id){
                    this.id = id
                    this.getPNameList()
                   getReq('/api/sys_role/vo/'+id,{}).then(res=>{
                       this.formObj = res.data.data
                       this.parentName = this.formObj.parentName
                   })
                }else {
                    this.getPNameList()
                }
                this.showSysRoleEdit=true
            },
            _pNameChange(data){
                this.formObj.pid=data;
            },
            cancel(){
                this.showSysRoleEdit = false
            },
            save(){

                this.$refs['sysRoleEditRef'].validate((valid) => {
                    if (valid) {
                        if (this.id!==undefined){
                            // 编辑
                            putReq('/api/sys_role/'+this.id,this.formObj).then(res=>{
                                if (res.data.data){
                                    this.$emit('page')
                                    this.showSysRoleEdit=false
                                    ElMessage.success({
                                        message: '添加成功！',
                                        type: 'success'
                                    });
                                }
                            })
                        }else{
                            postReq('/api/sys_role/add',this.formObj).then(res=>{
                                if (res.data.data){
                                    this.$emit('page')
                                    this.showSysRoleEdit=false
                                    ElMessage.success({
                                        message: '添加成功！',
                                        type: 'success'
                                    });
                                }
                            })
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            getPNameList(){
                this.pNameList = []
                if (this.id){
                    getReq('/api/sys_role/list_for_parent?id='+this.id,{}).then(res=>{
                        this.pNameList=res.data.data
                    })
                }else{
                    getReq('/api/sys_role/list',{}).then(res=>{
                        this.pNameList=res.data.data
                    })
                }

            },
        }
    }
</script>

<style scoped>

</style>